package com.elsa.smart_buffet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.elsa.smart_buffet.mapper.CategorySumMapper;
import com.elsa.smart_buffet.mapper.MenuMapper;
import com.elsa.smart_buffet.pojo.Menu;
import com.elsa.smart_buffet.pojo.ResultBox.CallBackMessage;
import com.elsa.smart_buffet.pojo.ResultBox.ResponseResult;
import com.elsa.smart_buffet.pojo.po.CategorySum;
import com.elsa.smart_buffet.pojo.vo.ClassifyTableStats;
import com.elsa.smart_buffet.service.MenuService;
import com.elsa.smart_buffet.utils.CategoryUtil;
import com.elsa.smart_buffet.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private CategoryUtil categoryUtil;

    @Autowired
    private CategorySumMapper categorySumMapper;

    @Override
    public ResponseResult selectAllMenu() {
        return new ResponseResult(CallBackMessage.SUCCESS, menuMapper.selectList(null));
    }

    @Override
    public ResponseResult getTopLook() {
        return new ResponseResult(CallBackMessage.SUCCESS, menuMapper.getTopLook());
    }

    @Override
    public ResponseResult selectMenuCount() {
        return new ResponseResult(CallBackMessage.SUCCESS, menuMapper.selectCount(null));
    }

    @Override
    public ResponseResult selectMenuCategoryCount() {
        return new ResponseResult(CallBackMessage.SUCCESS, menuMapper.selectMCategoryCount());
    }

    @Override
    public ResponseResult selectOneByIdMenu(int id) {
        return new ResponseResult(CallBackMessage.SUCCESS, menuMapper.selectById(id));
    }

    @Override
    public ResponseResult updateMenu(Menu menu) {
        return new ResponseResult(CallBackMessage.SUCCESS, menuMapper.updateById(menu));
    }

    @Override
    public ResponseResult deleteByIdMenu(int id) {
        return new ResponseResult(CallBackMessage.SUCCESS, menuMapper.deleteById(id));
    }

    @Override
    public ResponseResult insertMenu(Menu menu) {

        menu.setMTime(new Date(DateUtil.now().getTime()));
        log.info(String.valueOf(menu));
        return new ResponseResult(CallBackMessage.SUCCESS, menuMapper.insert(menu));
    }

    @Override
    public ResponseResult selectMenuByCategory(String category) {
        return new ResponseResult(CallBackMessage.SUCCESS, menuMapper.selectMenuByCategory(category));
    }

    @Override
    public ResponseResult selectMenuBySearchData(String data) {
        return new ResponseResult(CallBackMessage.SUCCESS, menuMapper.selectMenuBySearchData(data));
    }

    @Override
    public ResponseResult selectClassifyTable(String date) {
        int totalSUMCalorie = 0;
        List<CategorySum> listCategorySum = new ArrayList<>();
        for(int i = 1; i <= 4; i++) {
            CategorySum category = categorySumMapper.getCategorySumByTimeAndCategory(date, i + "");
            totalSUMCalorie += category.getSUMCalorie();
            listCategorySum.add(category);
            log.info(String.valueOf(category));
        }
        log.info(String.valueOf(totalSUMCalorie));
        List<ClassifyTableStats> list = new ArrayList<ClassifyTableStats>();
        for(int i = 1; i <= 4; i++) {
            list.add(new ClassifyTableStats(categoryUtil.getCategory(listCategorySum.get(i - 1).getCategory()),
                    listCategorySum.get(i - 1).getSUMCalorie(),
                    String.valueOf(Math.round((double) listCategorySum.get(i - 1).getSUMCalorie() / totalSUMCalorie * 10000) / 100) + '%'));
        }
        log.info(String.valueOf(list));
        return new ResponseResult(CallBackMessage.SUCCESS, list);
    }
}
